package com.shopx.config.service.impl;


import com.shopx.base.exception.OriginException;
import com.shopx.cmm.DTO.PageDTO;
import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.base.service.base.BaseService;
import com.shopx.config.bean.SysDict;
import com.shopx.config.dao.db1.SysDictDao;
import com.shopx.config.model.SysDictModel;
import com.shopx.config.service.ISysDictService;
import com.shopx.core.common.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * SysDict
 *
 * @author lituo 2021-01-04
 */
@Slf4j
@Service
public class SysDictServiceImpl extends BaseService implements ISysDictService {

  @Autowired
  private SysDictDao sysDictDao;

  @Autowired
  private ISysSeqService sysSeqService;

  @Override
  public SysDictModel selectByPrimaryKey(SysDict sysDict) throws ShopxException {
    if (sysDict == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    return sysDictDao.selectByPrimaryKey(sysDict);
  }

  @Override
  public Integer insertSelective(SysDict sysDict) throws ShopxException {
    if (sysDict == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    String id = sysSeqService.getTabSeq("SysDict", 4, null);
    sysDict.setState(CommonConstant.State.EFFECTIVE);
    sysDict.setDictId(id);
    int result = sysDictDao.insertSelective(sysDict);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  public Integer updateByPrimaryKeySelective(SysDict sysDict) throws ShopxException {
    if (sysDict == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    int result = sysDictDao.updateByPrimaryKeySelective(sysDict);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  public int deleteByPrimaryKey(SysDict sysDict) throws ShopxException {
    if (sysDict == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    int result = sysDictDao.deleteByPrimaryKey(sysDict);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  public void deleteBatch(List<SysDict> list) throws ShopxException{
    if(list==null || list.size()==0){
      throw new OriginException("至少删除一条记录");
    }
    sysDictDao.deleteBatch(list);
  }




  @Override
  public int selectCount(SysDictModel sysDict) throws ShopxException {
    return sysDictDao.selectCount(sysDict);
  }

  @Override
  public List<SysDictModel> selectList(SysDictModel sysDict) throws ShopxException {
    List<SysDictModel> list = sysDictDao.selectList(sysDict);
    return list;
  }

  @Override
  public PageDTO<SysDictModel> getListPage(SysDictModel sysDict) throws ShopxException {
    if (sysDict == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    PageDTO<SysDictModel> pageDTO = new PageDTO<SysDictModel>();
    pageDTO.setiTotalDisplayRecords(selectCount(sysDict));
    pageDTO.setData(selectList(sysDict));
    return pageDTO;
  }

  @Override
//  @Cacheable(value = CommonConstant.CacheKey.CMM, key = "'CONFIG_DICTVAL'+dictCode+dictName")
  public String queryValueByName(String dictCode, String dictName){
    SysDict sysDict = new SysDict();
    sysDict.setDictCode(dictCode);
    sysDict.setDictName(dictName);
    return sysDictDao.selectByName(sysDict).getDictValue();
  }

}

